<?php
require_once ("../src/db/connectdatabase.php");

function getNews($tnew, $start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "SELECT n.new_id, n.new_title, n.datecreate, t.nametypenew FROM news as n, typenews as t WHERE n.idtnew=t.idtnew AND n.idtnew LIKE '%".$tnew."%' AND t.isuse=1 ORDER BY datecreate DESC LIMIT ".$start.",".$lenght;
	
	return $con -> getvalueString($sql);
}

function getRowData($tnew) {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM news WHERE idtnew LIKE '%".$tnew."%'";
	return $con -> getvalueString($sql);
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}

$tnew = "";
if(isset($_GET['tn'])) {
	$tnew = $_GET['tn'];
}

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrcountrows = getRowData($tnew);

$sumrecord = $arrcountrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

$arrnews = getNews($tnew, ($page-1)*$numrecord, $numrecord);
?>
<style>
.class-lít-news-abc table {
	width: 900px;
	max-width: 900px;
}
.class-lít-news-abc table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.class-lít-news-abc table tr:hover td {
	background: #FFC;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.stylequestionname {
	width: 350px;
}

.styledatecreatequestion {
	width: 100px;
	max-width: 100px;
	text-align: center;
}

.styleacctionquestion {
	width: 150px;
	text-align: center;
	max-width: 150px;
}
.namequestionline {
	padding-left: 5px;
	padding-right: 5px;
	background: url(../images/bg-login.png);
	-moz-border-radius: 2px 2px 2px 2px;
	-webkit-border-radius: 2px 2px 2px 2px;
	border-radius: 2px 2px 2px 2px;
}

.stylestt {
	width: 50px;
	max-width: 50px;
	text-align: center;
}

.stylequestiontypenew {
	width: 150px;
	max-width: 150px;
	text-align: center;
}
</style>
<h3>Danh Sách Tin Tức</h3>
<div>
	<div style="width:900px; float:left;">	
    	<table>
        	<tr>
                <td>Loại Tin Tức</td>
                <td><select id="form_statussearch" style="height: 30px; width:200px; margin-right:10px; padding:5px"><option value="">--Tất Cả--</option></select></td>
                <td><input id="form_submitsearch" type="button" style="cursor:pointer ;background-image:url(../images/timkiem.png); width:70px; height:24px; border:0px;" value="" /></td>
            </tr>
        </table>
    </div>
</div>
<div class="class-lít-news-abc">
	<div><a href="admin.php?content=quanlykhac&&as=addnew"><img src="../images/icon-add.jpg" title="Thêm Tin Tức" />Thêm Tin Tức Mới</a></div>
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:10px;"></div>
	<table>
    	<tr>
        	<th class="title" style="width:20px"><input type="checkbox" class="checkall" title="Chọn Tất Cả" /></th>
            <th class="stylestt title">STT</th>
            <th class="stylequestiontypenew title">Loại Tin Tức</th>
            <th class="stylequestionname title">Tiêu Đề Tin Tức</th>
            <th class="styledatecreatequestion title">Ngày C.Nhật</th>
            <th class="styleacctionquestion title">Chức Năng</th>
        </tr>
        <?php
		$i = ($page-1)*$numrecord + 1;
		foreach ($arrnews as $entry) {
		?>
        <tr>
        	<td class="namequestionline"><input type="checkbox" value="<?php echo($entry[0]) ?>" title="Chọn để xóa" name="iddels[]" class="linecheckall" /></td>
            <td align="center" class="namequestionline"><?php echo($i) ?></td>
            <td class="namequestionline"><?php echo($entry[3]) ?></td>
        	<td class="namequestionline"><?php echo($entry[1]) ?></td>
            <td class="namequestionline" align="center"><?php echo(int_to_date($entry[2])) ?></td>
            <td class="namequestionline" align="center"><img idvalue="<?php echo($entry[0]) ?>" class="class-edit-link" src="../images/icon-edit.gif" title="Chỉnh Sửa" />&nbsp; | &nbsp;<img idvalue="<?php echo($entry[0]) ?>" class="class-delete-link" src="../images/icon-delete.gif" title="Xóa" /></td>
        </tr>
        <?php
		$i++;
		}
		?>
    </table>
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:10px;"></div>
    <div><a href="admin.php?content=quanlykhac&&as=addnew"><img src="../images/icon-add.jpg" title="Thêm Tin Tức" />Thêm Tin Tức Mới</a></div>
</div>

<script>
$(document).on('click', '.class-edit-link', function(e) {
	elem = $(this)
	var idcardtype = elem.attr('idvalue');
	window.location= 'admin.php?content=quanlykhac&&as=editnew&&id=' + idcardtype;
});

$(document).on('click', '.class-delete-link', function(e) {
	elem = $(this)
	var id = elem.attr('idvalue');
	if(confirm("Bạn có muốn xóa?")) {
		$.ajax({
			url: "../src/ajax/ajaxnews.php",
			type: "post",
			data: {act:'del', idre: id},
			dataType:"json",
			success: function(data) {
				if(data == true) {
					elem.parent().parent().fadeOut(300, function(){elem.parent().parent().remove();});
				} else {
					alert("Cập nhật dữ liệu không thành công, hoặc mất kết nối internet");
				}
			}
		});
	}
});


$(document).on('click', '.checkall', function(e) {
	if(!$(this).is(':checked')) {
		$('.checkall').attr('title', 'Chọn Tất Cả');
		$('.linecheckall').removeAttr('checked');
	} else {
		$('.checkall').attr('title', 'Bỏ Chọn Tất Cả');
		$('.linecheckall').attr('checked', 'checked');
	}
});

$(document).on('click', '.class-pageElem', function() {
	page = $(this).attr('page');
	var name = $('#form_statussearch').val();
	
	openwindowns('admin.php?content=quanlykhac&as=news&tn='+name+'&page=' + page);
});

function openwindowns(url) {
	window.location = url;
}

loadtypenews();
function loadtypenews() {
	$.ajax({
		url: "../src/ajax/ajaxAdminTypeNew.php",
		type: "post",
		data: {act:'getall'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + '</option>';
			});
			$('#form_statussearch').append(html);
			$('#form_statussearch').val("<?php echo($tnew) ?>");
		}
	});
}

$('#form_submitsearch').click(function(e) {
	var name = $('#form_statussearch').val();
	
	openwindowns('admin.php?content=quanlykhac&&as=news&tn=' + name);
});
</script>